package com.tma.timesheettracker.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.tma.timesheettracker.R;
import com.tma.timesheettracker.adapter.HistoryAdapter;
import com.tma.timesheettracker.model.TrackingEntry;
import com.tma.timesheettracker.util.Util;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class HistoryActivity extends Activity {

	public static final int REQUEST_CODE = 1111;
	private ListView m_lstHistory;
	private List<List<TrackingEntry>> m_lstEntrySorted =new ArrayList<List<TrackingEntry>>();
	private List<TrackingEntry> m_lstEntry=new ArrayList<TrackingEntry>();
	private int m_oneHourSize = 0;

	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_history);
		m_lstHistory = (ListView) findViewById(R.id.lstHistory);
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			m_lstEntrySorted = (List<List<TrackingEntry>>) bundle.getSerializable(Util.SORTED_TIME_SHEET_TRACKING);
			m_oneHourSize = bundle.getInt(Util.ONE_HOUR_SIZE);
			m_lstEntry = (List<TrackingEntry>) bundle.getSerializable(Util.TIME_SHEET_TRACKING);
		}
		m_lstHistory.setAdapter(null);
		m_lstHistory.setAdapter(new HistoryAdapter(this, m_lstEntrySorted));
		m_lstHistory.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(HistoryActivity.this, PreviousActivity.class);
				intent.putExtra(Util.ONE_HOUR_SIZE, m_oneHourSize);
				intent.putExtra(Util.TIME_SHEET_TRACKING, (Serializable) m_lstEntrySorted.get(position));
				startActivityForResult(intent, RecordWorkActivity.REQUEST_CODE);
			}
		});
		getActionBar().setHomeButtonEnabled(true);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			onBackPressed();
			return true;
		case R.id.addTask:
			Intent intent = new Intent(HistoryActivity.this, RecordWorkActivity.class);
			intent.putExtra(Util.ACTION,Util.CREATE);
			intent.putExtra(Util.DAY_TO_RECORD, Util.getCurrentDay());
			intent.putExtra(Util.TIME_SHEET_TRACKING, (Serializable) m_lstEntry);
			startActivityForResult(intent, RecordWorkActivity.REQUEST_CODE);
			return true;
		}
		return super.onOptionsItemSelected(item);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == RecordWorkActivity.REQUEST_CODE && resultCode == Util.ACTION_CLOSE) {
			setResult(Util.ACTION_CLOSE);
			finishActivity(REQUEST_CODE);
			finish();
		} else {
			System.out.println("PREVIOUS GOT: " + requestCode + "; " + resultCode + "; " + data);
		}
	}
}
